<?php
/*
 * @Author: CYH
 * @Date: 2020-10-31 10:25:06
 * @LastEditTime: 2021-05-08 16:31:33
 * @LastEditors: Please set LastEditors
 * @Description: 房间管理control类
 * @FilePath: \application\controllers\Room\Room.php
 */
// 指定允许其他域名访问  
header('Access-Control-Allow-Origin:*');
// 响应类型  
header('Access-Control-Allow-Methods:POST');
// 响应头设置  
header('Access-Control-Allow-Headers:x-requested-with,content-type:application/json,charset:utf-8');
require_once 'PHPExcel-1.8\Classes\PHPExcel.php';
require_once 'PHPExcel-1.8\Classes\PHPExcel\IOFactory.php';
// require_once 'PHPExcel\Classes\PHPExcel\Reader\Excel5.php';
require_once 'PHPExcel-1.8\Classes\PHPExcel\Reader\Excel2007.php';

// 房间管理control类
class Room extends CI_Controller
{
    //构造方法
    public function __construct()
    {

        parent::__construct();
        $this->load->model('Room/Room_manage');
        $this->load->helper('url_helper');
        $this->load->library('session');
        // $this->load->helper('date');
        $this->load->helper(array('form', 'url', 'date'));
    }

    /**
     * @description: 房间管理查询方法
     * @param {area_number, floor_number, room_sex, room_number, liveNumber, remainNumber, limit}
     * @return {*}
     */    
    public function room_select()
    {

        $area_number = $_POST['area_number'];       //区域编号
        $floor_number = $_POST['floor_number'];     //宿舍楼编号
        $room_sex = $_POST['room_sex'];             //男/女宿舍
        $room_number = $_POST['room_number'];       //宿舍房间号
        $liveNumber = $_POST['liveNumber'];         //宿舍可住人数
        $remainNumber = $_POST['remainNumber'];     //宿舍剩余可住人数
        $limit = $_POST['page'];                    //页数

        // $area_number = "";
        // $floor_number = "";
        // $room_sex = "";
        // $room_number = "A04";
        // $liveNumber = "";
        // $remainNumber = "";
        // $limit = "1";

        //房间管理查询方法-总条数
        $count = $this->Room_manage->room_select_all(
            $area_number,
            $floor_number,
            $room_sex,
            $room_number,
            $liveNumber,
            $remainNumber,
            $limit
        );
            
        $result_json = $this->Room_manage->room_select(
            $area_number,
            $floor_number,
            $room_sex,
            $room_number,
            $liveNumber,
            $remainNumber,  
            $limit,
            $count
        );

        echo $result_json;
        return 0;
    }

    /**
     * @description:房间管理修改方法 
     * @param {area_number, floor_number, room_number, roomNumber_after, dorm_floor, room_type, room_status, temporary, room_sex,costman, liveNumber, cost }
     * @return {*}
     */    
    public function room_update()
    {

        $area_number = $_POST['area_number'];             //区域编号
        $floor_number = $_POST['floor_number'];           //宿舍楼编号
        $room_number = $_POST['room_number'];             //更改前宿舍编号
        $roomNumber_after = $_POST['roomNumber_after'];   //更改后宿舍编号
        $dorm_floor = $_POST['dorm_floor'];               //宿舍楼层号
        $room_type = $_POST['room_type'];                 //宿舍房间类型
        $room_status = $_POST['room_status'];             //宿舍状态
        $temporary = $_POST['temporary'];                 //是否临时
        $room_sex = $_POST['room_sex'];                   //男/女宿舍
        $costman = $_POST['costman'];                     //住宿费用承担人
        $liveNumber = $_POST['liveNumber'];               //宿舍可住人数
        $cost = $_POST['cost'];                           //住宿费用

        // $area_number = "1585"; 
        // $floor_number = "1"; 
        // $room_number = "601-2"; 
        // $roomNumber_after = "";
        // $dorm_floor = "6"; 
        // $room_type = "1"; 
        // $room_status = "1"; 
        // $temporary = "0";
        // $room_sex = "1";
        // $costman = "员工"; 
        // $liveNumber = "1"; 
        // $cost = "0"; 

        $result_json = $this->Room_manage->room_update(
            $area_number,
            $floor_number, 
            $room_number, 
            $roomNumber_after,
            $dorm_floor, 
            $room_type, 
            $room_status, 
            $temporary,
            $room_sex,
            $costman, 
            $liveNumber, 
            $cost 
        );

        echo $result_json;

        return 0;
    }

    /**
     * @description: 房间管理新增方法
     * @param {area_number, floor_number, dorm_floor, room_number, room_type, room_sex, liveNumber, cabinet_number, room_status, temporary, cost, costman}
     * @return {*}
     */    
    public function room_insert()
    {
        
        $area_number = $_POST['area_number'];          //区域编号
        $floor_number = $_POST['floor_number'];        //宿舍楼编号
        $dorm_floor = $_POST['dorm_floor'];            //宿舍楼层号
        $room_number = $_POST['room_number'];          //宿舍房间号
        $room_type = $_POST['room_type'];              //宿舍房间类型
        $room_sex = $_POST['room_sex'];                //男/女宿舍
        $liveNumber = $_POST['liveNumber'];            //宿舍可住人数
        //$cabinet_number = $_POST['cabinet_number'];  //宿舍柜子数量
        $room_status = $_POST['room_status'];          //宿舍状态
        $temporary = $_POST['temporary'];              //是否临时
        $cost = $_POST['cost'];                        //住宿费用
        $costman = $_POST['costman'];                  //宿舍费用承担人

        // $area_number = "1585"; 
        // $floor_number = "1"; 
        // $dorm_floor = "6"; 
        // $room_number = "670"; 
        // $room_type = "宿舍"; 
        // $room_sex  = "1"; 
        // $liveNumber = "4"; 
        // $room_status = "1"; 
        // $temporary  = "0"; 
        // $cost = "0"; 
        // $costman = "员工"; 

        $result_json = $this->Room_manage->room_insert(
            $area_number,
            $floor_number,
            $dorm_floor,
            $room_number,
            $room_type, 
            $room_sex,
            $liveNumber,
            // $cabinet_number,
            $room_status,
            $temporary,
            $cost,
            $costman
        );

        echo $result_json;

        return 0;
    }

    /**
     * @description:房间管理删除方法 
     * @param {area_number, floor_number, room_number}
     * @return {*}
     */    
    public function room_delete()
    {

        $area_number = $_POST['area_number'];   //区域编号
        $floor_number = $_POST['floor_number']; //宿舍楼编号
        $room_number = $_POST['room_number'];   //宿舍房间号

        // $area_number = "1585";
        // $floor_number = "1";
        // $room_number = "670";

        $result_json = $this->Room_manage->room_delete(
            $area_number,
            $floor_number,
            $room_number
        );

        echo $result_json;

        return 0;
    }

    /**
     * @description: 房间初始化登陆方法
     * @param {initPassword}
     * @return {*}
     */    
    public function room_initialization()
    {

        $initPassword = $_POST['initPassword']; //密码

        // $initPassword = "123456"; //密码

        $result_json = $this->Room_manage->room_initialization($initPassword);

        echo $result_json;

        return 0;
    }

    /**
     * @description: 房间初始化表单方法
     * @param {area_number, floor_number, floor1, floor2, room_type, room_sex, room_Prefix, liveNumber, cabinet_number, cost, costman, room_status, temporary, roomNumber}
     * @return {*}
     */    
    public function room_form()
    {

        $area_number = $_POST['area_number'];       //区域编号
        $floor_number = $_POST['floor_number'];     //宿舍楼编号
        $floor1 = $_POST['floor1'];                 //宿舍楼层1
        $floor2 = $_POST['floor2'];                 //宿舍楼层2
        $room_type = $_POST['room_type'];           //宿舍类型
        $room_sex = $_POST['room_sex'];             //男/女宿舍
        $room_Prefix = $_POST['room_Prefix'];       //房间号前缀
        $liveNumber = $_POST['liveNumber'];         //宿舍可住人数
        $cabinet_number = $_POST['cabinet_number']; //柜子数量
        $cost = $_POST['cost'];                     //宿舍费用
        $costman = $_POST['costman'];               //费用承担者
        $room_status = $_POST['room_status'];       //宿舍状态
        $temporary = $_POST['temporary'];           //是否临时
        $roomNumber = $_POST['roomNumber'];         //房间数量

        // $area_number = "1585";
        // $floor_number = "1";
        // $floor1 = "3";
        // $floor2 = "4";
        // $room_type = "1";
        // $room_sex = "1";
        // $room_Prefix = "";
        // $liveNumber = "6";
        // $cabinet_number = "6";
        // $cost = "0";
        // $costman = "员工";
        // $room_status = "1";
        // $temporary = "0";
        // $roomNumber = "10";

        $result_json = $this->Room_manage->room_form(
            $area_number,
            $floor_number,
            $floor1,
            $floor2,
            $room_type,
            $room_sex,
            $room_Prefix,
            $liveNumber,
            $cabinet_number,
            $cost,
            $costman,
            $room_status,
            $temporary,
            $roomNumber
        );

        echo $result_json;

        return 0;
    }

    /**
     * @description: 查询固定房间类型无人入住空房间
     * @param {room_type, room_sex}
     * @return {*}
     */ 
    public function no_live_room()
    {
        $area_number = $_POST['area_number'];   //区域编码
        $floor_number = $_POST['floor_number']; //宿舍楼编码
        $dorm_floor = $_POST['dorm_floor'];     //楼层号
        $room_type = $_POST['room_type'];       //房间类型
        $room_sex = $_POST['room_sex'];         //房间性别

        // $area_number = '1585';
        // $floor_number = '1';
        // $dorm_floor = '';
        // $room_type = '2';
        // $room_sex = '1';

        $result_json = $this->Room_manage->no_live_room(
            $area_number,
            $floor_number,
            $dorm_floor,
            $room_type, 
            $room_sex
        );
        
        echo $result_json;

        return 0;
    }
    /**
     * @description: 宿舍床位信息excel导出
     * @param {*}
     * @return {*}
     */    
    public function excel_room()
    {        
        // 创建一个处理对象实例   
        $objExcel = new \PHPExcel();  
        // 创建文件格式写入对象实例, uncomment   
        $objProps = $objExcel->getProperties();  
        //设置创建者  TODO:
        $objProps->setCreator('admin');
        //描述  
        // $objProps->setDescription("入住信息汇总");  
        $objActSheet = $objExcel->getActiveSheet();
        //单元格赋值    
        $objActSheet->setCellValue('A1','区域名称');
        $objActSheet->setCellValue('B1','宿舍楼名称');
        $objActSheet->setCellValue('C1','楼栋号');
        $objActSheet->setCellValue('D1','宿舍楼层号');
        $objActSheet->setCellValue('E1','宿舍房间号');
        $objActSheet->setCellValue('F1','房间类型');
        $objActSheet->setCellValue('G1','房间状态');
        $objActSheet->setCellValue('H1','临时入住');
        $objActSheet->setCellValue('I1','房间入住类型');
        $objActSheet->setCellValue('J1','费用承担');
        $objActSheet->setCellValue('K1','可住人数');
        $objActSheet->setCellValue('L1','已住人数');
        $objActSheet->setCellValue('M1','住宿费用');
    
        $num = 2;
        
        $data = $this->Room_manage->excel_room();
        $data = json_decode($data,true);
        
        for ($i=0; $i < count($data['msg']); $i++) 
        { 
            //宿舍类型
            if($data['msg'][$i]['room_type'] == 0)
            {
                $data['msg'][$i]['room_type'] = '单人间';
            } 
            else 
            {
                $data['msg'][$i]['room_type'] = '多人间';
            }
                    
            if($data['msg'][$i]['room_status'] == 0)
            {
                $data['msg'][$i]['room_status'] = '未开启';
            } 
            else 
            {
                $data['msg'][$i]['room_status'] = '开启';
            }
        
            if($data['msg'][$i]['temporary'] == 'X') 
            {
                $data['msg'][$i]['temporary'] = '是';
            } 
            else 
            {
                $data['msg'][$i]['temporary'] = '否';
            }
        
            if($data['msg'][$i]['room_sex'] == 0)
            {
                $data['msg'][$i]['room_sex'] = '女';
            } 
            else 
            {
                $data['msg'][$i]['room_sex'] = '男';
            }
    
            if($data['msg'][$i]['costman'] == 0)
            {
                $data['msg'][$i]['costman'] = '公司承担';
            } 
            else 
            {
                $data['msg'][$i]['costman'] = '员工承担';
            }
        }
        
        for ($i=0; $i < count($data['msg']); $i++) 
        { 
            $objActSheet->setCellValueExplicit('A' . $num,$data['msg'][$i]['area_name'] , PHPExcel_Cell_DataType::TYPE_STRING);           //区域名称
            $objActSheet->setCellValueExplicit('B' . $num,$data['msg'][$i]['floor_name'] , PHPExcel_Cell_DataType::TYPE_STRING);          //宿舍楼名称
            $objActSheet->setCellValueExplicit('C' . $num,$data['msg'][$i]['caveNumber'] , PHPExcel_Cell_DataType::TYPE_STRING);          //楼栋号
            $objActSheet->setCellValueExplicit('D' . $num,$data['msg'][$i]['dorm_floor'] , PHPExcel_Cell_DataType::TYPE_STRING);          //宿舍楼层号
            $objActSheet->setCellValueExplicit('E' . $num,$data['msg'][$i]['room_number'] , PHPExcel_Cell_DataType::TYPE_STRING);         //宿舍房间号 
            $objActSheet->setCellValueExplicit('F' . $num,$data['msg'][$i]['room_type'] , PHPExcel_Cell_DataType::TYPE_STRING);           //宿舍类型
            $objActSheet->setCellValueExplicit('G' . $num,$data['msg'][$i]['room_status'], PHPExcel_Cell_DataType::TYPE_STRING);          //房间状态
            $objActSheet->setCellValueExplicit('H' . $num,$data['msg'][$i]['temporary'], PHPExcel_Cell_DataType::TYPE_STRING);            //是否临时
            $objActSheet->setCellValueExplicit('I' . $num,$data['msg'][$i]['room_sex'], PHPExcel_Cell_DataType::TYPE_STRING);             //入住性别
            $objActSheet->setCellValueExplicit('J' . $num,$data['msg'][$i]['costman'], PHPExcel_Cell_DataType::TYPE_STRING);              //费用承担人
            $objActSheet->setCellValueExplicit('K' . $num,$data['msg'][$i]['liveNumber'], PHPExcel_Cell_DataType::TYPE_STRING);           //可住人数
            $objActSheet->setCellValueExplicit('L' . $num,$data['msg'][$i]['peopleNumber'], PHPExcel_Cell_DataType::TYPE_STRING);         //实际人数
            $objActSheet->setCellValueExplicit('M' . $num,$data['msg'][$i]['cost'], PHPExcel_Cell_DataType::TYPE_STRING);                 //住宿费用
            //设置行的属性  
            $objActSheet->getRowDimension($num)->setRowHeight('20px');//设置行高  
            $num = $num + 1;
        }
        
        //设置列的属性  
        $objActSheet->getColumnDimension("A")->setWidth(20); // 设置宽度  
        $objActSheet->getColumnDimension("B")->setWidth(20); // 设置宽度  
        $objActSheet->getColumnDimension("C")->setWidth(20); // 设置宽度  
        $objActSheet->getColumnDimension("D")->setWidth(20); // 设置宽度  
        $objActSheet->getColumnDimension("E")->setWidth(20); // 设置宽度  
        $objActSheet->getColumnDimension("F")->setWidth(20); // 设置宽度 
        $objActSheet->getColumnDimension("G")->setWidth(20); // 设置宽度
        $objActSheet->getColumnDimension("H")->setWidth(20); // 设置宽度 
        $objActSheet->getColumnDimension("I")->setWidth(20); // 设置宽度 
        $objActSheet->getColumnDimension("J")->setWidth(20); // 设置宽度 
        $objActSheet->getColumnDimension("K")->setWidth(20); // 设置宽度 
        $objActSheet->getColumnDimension("L")->setWidth(20); // 设置宽度 
        $objActSheet->getColumnDimension("M")->setWidth(20); // 设置宽度 
        $outputFileName = iconv('UTF-8', 'gb2312', '宿舍房间信息.xlsx');  
        //文件直接输出到浏览器  
        header('Pragma:public');  
        header('Expires:0');  
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');  
        header('Content-Type:application/force-download');  
        header('Content-Type:application/vnd.ms-excel');  
        header('Content-Type:application/octet-stream');  
        header('Content-Type:application/download');  
        header('Content-Disposition:attachment;filename=' . $outputFileName);  
        header('Content-Transfer-Encoding:binary');  
        PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007')->save('php://output');
                
        //设置对齐方式      
        $objStyleA3 = $objActSheet->getStyle('A3');  
        $objAlignA3 = $objStyleA3->getAlignment();  
        $objAlignA3->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);  
        $objAlignA3->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        
        $objExcel = null;
        }
}
